package com.dci.controller.schedule;

import com.dci.context.Config;
import com.dci.model.schedule.ProgressEntering;
import com.dci.result.JsonData;
import com.dci.result.PageResult;
import com.dci.result.ResultInfo;
import com.dci.result.ResultUtil;
import com.dci.service.schedule.ProgressEntertingService;
import com.google.gson.Gson;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * All rights Reserved, Designed By 姑苏区政府大数据中心
 *
 * @version V1.0
 * @Title: null.java
 * @Package com.dci.controller.project
 * @Description: 业务录入
 * @author: 张星亮
 * @date: 2019/4/21
 * @Copyright: 2019 姑苏区政府大数据中心. All rights reserved.
 * 注意：本内容仅限于姑苏区政府大数据中心内部传阅，禁止外泄以及用于其他的商业目
 */
@Controller
@RequestMapping("/progressEntering")
public class PropressEnteringController {

    @Resource
    private ProgressEntertingService progressEntertingService;

    @Value("${upload.path}")
    private String path;
    @Value("${upload.url}")
    private String url;
    @Value("${upload.urlP}")
    private String urlP;

    /**
     * 新增需录入项
     *
     * @param progressEntering
     * @return ResultInfo 封装的返回参数  保存成功msg:906 保存失败msg:911
     */
    @PostMapping("/add")
    @ResponseBody
    public ResultInfo add(@RequestBody ProgressEntering progressEntering) {
        try {
            return progressEntertingService.insert(progressEntering);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.createSuccess(Config.MESSAGE, 911, null);
        }
    }

    /**
     * 资金拨付, 更新数据
     *
     * @param files
     * @param form
     * @return JsonData 封装的返回参数  保存成功msg:906 保存失败msg:911
     */
    @RequestMapping("/insertAdd")
    @ResponseBody
    public JsonData insertAdd(@RequestParam("uploadFile") MultipartFile[] files, @RequestParam("form") String form) {
        Gson gson = new Gson();
        ProgressEntering progressEntering = gson.fromJson(form, ProgressEntering.class);
        Date now = new Date();
        SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
        String address = df.format(now);
        String filePath = path + File.separator + address + "/";
        System.out.println(filePath);
        String fileNames = "";
        if (files.length != 0) {
            for (MultipartFile file : files) {
                String fileName = file.getOriginalFilename();
                if (fileNames == "") {
                    fileNames += fileName;
                } else {
                    fileNames += ',' + fileName;
                }
                File fileDir = new File(filePath);
                if (!fileDir.exists()) {
                    fileDir.mkdirs();
                }
                try {
                    file.transferTo(new File(fileDir.getAbsolutePath(), fileName));
                } catch (IOException e) {
                    continue;
                }
            }
        }
        String urlPath = urlP + address + "/";
        progressEntering.setProof_file_path(urlPath);
        progressEntering.setProof_file_name(fileNames);

        try {
            return progressEntertingService.insertAdd(progressEntering);
        } catch (Exception e) {
            e.printStackTrace();
            return JsonData.success(null, 911);
        }
    }

    /**
     * 业务录入
     *
     * @param progressEntering
     * @return JsonData 封装的返回参数  保存成功msg:906 保存失败msg:911
     */
    @PostMapping("/entering")
    @ResponseBody
    public JsonData update(@RequestBody ProgressEntering progressEntering) {
        try {
            return progressEntertingService.update(progressEntering);
        } catch (Exception e) {
            e.printStackTrace();
            return JsonData.success(null, 911);
        }
    }

    /**
     * 业务审核
     *
     * @param id
     * @param audit_status
     * @return ResultInfo 封装的返回参数  保存成功msg:906 保存失败msg:911
     */
    @PostMapping("/audit")
    @ResponseBody
    public ResultInfo update(@RequestParam("id") String id, @RequestParam("audit_status") int audit_status, @RequestParam("verify_opinion") String verify_opinion) {
        try {
            return progressEntertingService.audit(id, audit_status, verify_opinion);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.createSuccess(Config.MESSAGE, 911, null);
        }
    }

    /**
     * 业务详情
     *
     * @param id
     * @return JsonData 封装的返回参数  保存成功msg:906 保存失败msg:911
     */
    @PostMapping("/detail")
    @ResponseBody
    public JsonData selectById(@RequestParam("id") String id) {
        try {
            return progressEntertingService.selectById(id);
        } catch (Exception e) {
            e.printStackTrace();
            return JsonData.success(null, 911);
        }

    }

    /**
     * 根据code查询业务详情
     *
     * @param code
     * @return JsonData 封装的返回参数  保存成功msg:906 保存失败msg:911
     */
    @PostMapping("/detail1")
    @ResponseBody
    public JsonData selectByCode(@RequestParam("code") String code) {
        try {
            return progressEntertingService.selectByCode(code);
        } catch (Exception e) {
            e.printStackTrace();
            return JsonData.success(null, 911);
        }

    }

    /**
     * 列表
     *
     * @param
     * @return PageResult 封装的返回参数  保存成功msg:906 保存失败msg:911
     */
    @PostMapping("/list")
    @ResponseBody
    public PageResult select(@RequestBody ProgressEntering progressEntering) {
        try {
            return progressEntertingService.select(progressEntering);
        } catch (Exception e) {
            e.printStackTrace();
            return PageResult.fail(911);
        }

    }

    /**
     * 提交表单和上传文件
     *
     * @param
     * @return JsonData 封装的返回参数  保存成功msg:906 保存失败msg:911
     */
    @PostMapping("/enteringAndSave")
    @ResponseBody
    public JsonData enteringAndSave(@RequestParam("item_amount_file") MultipartFile[] files, @RequestParam("item_file") MultipartFile[] files1, @RequestParam("contract_file") MultipartFile[] files2, @RequestParam("project_change_file") MultipartFile[] files3, @RequestParam("form") String form) {
        Gson gson = new Gson();
        ProgressEntering progressEntering = gson.fromJson(form, ProgressEntering.class);
        String fileNames = "";
        String fileNames1 = "";
        String fileNames2 = "";
        String fileNames3 = "";

        Date now = new Date();
        SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
        String address = df.format(now);
        String filePath = path + File.separator + address + "/";

        if (files.length != 0) {
            for (MultipartFile file : files) {
                String fileName = file.getOriginalFilename();
                if (fileNames == "") {
                    fileNames += fileName;
                } else {
                    fileNames += ',' + fileName;
                }
                File fileDir = new File(filePath);
                if (!fileDir.exists()) {
                    fileDir.mkdirs();
                }
                try {
                    file.transferTo(new File(fileDir.getAbsolutePath(), fileName));
                } catch (IOException e) {
                    continue;
                }
            }
        }
        if (files1.length != 0) {
            for (MultipartFile file1 : files1) {
                String fileName1 = file1.getOriginalFilename();
                if (fileNames1 == "") {
                    fileNames1 += fileName1;
                } else {
                    fileNames1 += ',' + fileName1;
                }
                File fileDir = new File(filePath);
                if (!fileDir.exists()) {
                    fileDir.mkdirs();
                }
                try {
                    file1.transferTo(new File(fileDir.getAbsolutePath(), fileName1));
                } catch (IOException e) {
                    continue;
                }
            }
        }

        if (files2.length != 0) {
            for (MultipartFile file2 : files2) {
                String fileName2 = file2.getOriginalFilename();
                if (fileNames2 == "") {
                    fileNames2 += fileName2;
                } else {
                    fileNames2 += ',' + fileName2;
                }
                File fileDir = new File(filePath);
                if (!fileDir.exists()) {
                    fileDir.mkdirs();
                }
                try {
                    file2.transferTo(new File(fileDir.getAbsolutePath(), fileName2));
                } catch (IOException e) {
                    continue;
                }
            }
        }
        if (files3.length != 0) {
            for (MultipartFile file3 : files3) {
                String fileName3 = file3.getOriginalFilename();
                if (fileNames3 == "") {
                    fileNames3 += fileName3;
                } else {
                    fileNames3 += ',' + fileName3;
                }
                File fileDir = new File(filePath);
                if (!fileDir.exists()) {
                    fileDir.mkdirs();
                }
                try {
                    file3.transferTo(new File(fileDir.getAbsolutePath(), fileName3));
                } catch (IOException e) {
                    continue;
                }
            }
        }
        String urlPath = urlP + address + "/";
//        progressEntering.setItem_amount_file(fileNames);
//        progressEntering.setItem_file(fileNames1);
//        progressEntering.setContract_file(fileNames2);
//        progressEntering.setProject_change_file(fileNames3);
//        progressEntering.setFile_path(urlPath);

        return progressEntertingService.update(progressEntering);
    }

    /**
     * 业务录入管理删除 (逻辑删除)
     *
     * @param list
     * @return PageResult 封装的返回参数  保存成功msg:906 保存失败msg:911
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public PageResult deleteProject(@RequestBody List<ProgressEntering> list) {
        try {
            PageResult result = progressEntertingService.delete(list);
            return result;
        } catch (Exception e) {
            return PageResult.fail(911);
        }
    }

    /**
     * 查询所有
     *
     * @param
     * @return list集合
     */
    @PostMapping(value = "/selectAll")
    @ResponseBody
    public List<ProgressEntering> selectAll() {
        return progressEntertingService.selectAll();
    }


    /***
     * 导入数据
     * @param simple
     * @param progressEntering
     * @return ResultInfo 封装的返回参数  保存成功msg:906 保存失败msg:911
     */
    @PostMapping("/importData")
    @ResponseBody
    public ResultInfo uploadAndSave(@RequestParam("simple") String simple, @RequestParam("progressEntering") String progressEntering) {
        ResultInfo resultInfo = new ResultInfo();
        try {
            resultInfo = progressEntertingService.json(simple, progressEntering);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return resultInfo;
    }
}
